package com.feicheng.lanniao.service.impl;

import com.feicheng.lanniao.entity.Car;
import com.feicheng.lanniao.entity.CarRentClientVo;
import com.feicheng.lanniao.entity.CarRentInfo;
import com.feicheng.lanniao.entity.CarRentOwner;
import com.feicheng.lanniao.entity.CarRentalInfo;
import com.feicheng.lanniao.exception.SystemException;
import com.feicheng.lanniao.mapper.CarRentInfoMapper;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.ICarRentClientVoService;
import com.feicheng.lanniao.service.ICarRentInfoService;
import com.feicheng.lanniao.service.ICarRentOwnerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-09-07
 */
@Service
public class CarRentInfoServiceImpl extends ServiceImpl<CarRentInfoMapper, CarRentInfo> implements ICarRentInfoService {
	@Autowired
    private ICarRentClientVoService iCarRentClientVoService;
	@Autowired
	private ICarRentInfoService iCarRentInfoService;
	@Autowired
	private ICarRentOwnerService iCarRentOwnerService;
	/**
	 * @see.ICarRentInfoService.RentcarList
	 */
	@Override
	public ResultInfo RentcarList(String title, String details,Integer clientId ,Integer currentPage, Integer pageSize) {
		
		// TODO Auto-generated method stub
		QueryWrapper<CarRentClientVo> queryWrapper = new QueryWrapper<CarRentClientVo>();
		if(StringUtils.hasLength(title)) {
			queryWrapper.like("title", title);
		}
		if (StringUtils.hasLength(details)) {
			queryWrapper.like("details", details);
		}
		if(null != clientId && 0 != clientId) {
			queryWrapper.eq("client_id",clientId);
		}
		
		IPage<CarRentClientVo> pageInfo = new Page<CarRentClientVo>(currentPage, pageSize);
		return ResultInfo.success(iCarRentClientVoService.page(pageInfo, queryWrapper));
	}
	/**
	 * @see.ICarRentInfoService.selectRentCarById
	 */
	@Override
	public ResultInfo selectRentCarById(Integer crId) {
		// TODO Auto-generated method stub
		QueryWrapper<CarRentClientVo> cQueryWrapper = new QueryWrapper<CarRentClientVo>();
		cQueryWrapper.eq("cr_id", crId);
		return ResultInfo.success(iCarRentClientVoService.getOne(cQueryWrapper));
	}
	/**
	 * @see.ICarRentInfoService.addOneRent
	 */
	@Override
	public ResultInfo addOneRent(String tel,String wxNumber,String title,String details,Integer clientId) {
		if(null ==clientId || clientId==0) {
			throw new SystemException("clientId不能为空");
		}
		if (null == tel || tel.length() != 11) {
			throw new SystemException("请输入11位手机号");
		}
		if (null == wxNumber) {
			throw new SystemException("请输入微信号");
		}
		if (null == title) {
			throw new SystemException("请输入标题");
		}
		CarRentInfo carRentInfo = new CarRentInfo();
		carRentInfo.setTitle(title);
		carRentInfo.setDetails(details);
		carRentInfo.setWxNumber(wxNumber);
		carRentInfo.setTel(tel);
		iCarRentInfoService.save(carRentInfo);
		CarRentOwner carRentOwner = new CarRentOwner();
		// TODO Auto-generated method stub
		carRentOwner.setClientId(clientId);
		carRentOwner.setCrId(carRentInfo.getCrId());
		iCarRentOwnerService.save(carRentOwner);
		return ResultInfo.success();
	}
	/**
	 * @see.ICarRentInfoService.deleteRent
	 */
	@Override
	public ResultInfo deleteRent(Integer crId) {
		// TODO Auto-generated method stub
		//如果crId为空或0，抛出异常
		if(null == crId || 0 ==crId) {
			throw new SystemException("crId为空");
		}
		//构建查询条件
		QueryWrapper<CarRentInfo> queryWrapper = new QueryWrapper<CarRentInfo>();
		queryWrapper.eq("cr_id",crId);
		//创建CarRentInfo对象
		CarRentInfo carRentInfo = this.getById(crId);
		carRentInfo.setDelFlag(false);
		Boolean b = this.updateById(carRentInfo);
		if(!b) {
			throw new SystemException("删除失败");
		}
		
		return ResultInfo.success();
	}
	/**
	 * @see.ICarRentInfoService.updateRent
	 */
	@Override
	public ResultInfo updateRent(CarRentInfo carRentInfo) {
		// TODO Auto-generated method stub
		if(null == carRentInfo.getCrId()) {
			throw new SystemException("Id缺失 无法更新");
		}
		if (null == carRentInfo.getTel()) {
			throw new SystemException("请输入11位手机号");
		}
		if (null ==carRentInfo.getWxNumber()) {
			throw new SystemException("请输入微信号");
		}
		if (null == carRentInfo.getTitle()) {
			throw new SystemException("请输入标题");
		}
		this.updateById(carRentInfo);
		return ResultInfo.success();
	}

}
